package com.github.mapboxdemo;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;

import com.mapbox.mapboxsdk.camera.CameraUpdateFactory;
import com.mapbox.mapboxsdk.geometry.LatLng;
import com.mapbox.mapboxsdk.maps.MapView;
import com.mapbox.mapboxsdk.maps.MapboxMap;
import com.mapbox.mapboxsdk.maps.OnMapReadyCallback;
import com.mapbox.mapboxsdk.style.layers.PropertyFactory;
import com.mapbox.mapboxsdk.style.layers.RasterLayer;
import com.mapbox.mapboxsdk.style.sources.RasterSource;
import com.mapbox.mapboxsdk.style.sources.TileSet;

/**
 * Created by Administrator on 2017/1/18 0018.
 */

public class WmsActivity extends AppCompatActivity {
    private MapView mapView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mapView = (MapView) findViewById(R.id.mapview);

        mapView.onCreate(savedInstanceState);

        mapView.setStyleUrl("asset://hhxy_wms_style.json");
        //"http://192.168.4.240/map/{z}/{x}/{y}.png"
        //https://geodata.state.nj.us/imagerywms/Natural2015?bbox={bbox-epsg-3857}&format=image/png&service=WMS&version=1.1.1&request=GetMap&srs=EPSG:3857&width=256&height=256&layers=Natural2015
        //http://192.168.4.240/geoserver/wms?request=GetLegendGraphic&format=image/png&width=20&height=20&layer=hhxy:map_green_poylgon
        mapView.getMapAsync(new OnMapReadyCallback() {
            @Override
            public void onMapReady(MapboxMap mapboxMap) {
                TileSet tileSet=new TileSet("2.1.0", "http://a.tile.osm.org/{z}/{x}/{y}.png");
                TileSet tileSet2=new TileSet("2.1.0","http://192.168.4.240/map/{z}/{x}/{y}.png");
                tileSet2.setScheme("tms");

                RasterSource baseMapSource = new RasterSource("base-map-source",tileSet, 256);
                RasterSource overMapSource = new RasterSource("over-map-source",tileSet2, 256);

                mapboxMap.addSource(baseMapSource);
                mapboxMap.addSource(overMapSource);

                // Add the web map source to the map.
                RasterLayer webMapLayer = new RasterLayer("base-map-layer", "base-map-source");
                RasterLayer overMapLayer = new RasterLayer("over-map-layer", "over-map-source");

                //mapboxMap.addLayer(webMapLayer, "base-layer");
                mapboxMap.addLayer(overMapLayer, "over-layer");

                mapboxMap.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(30.6662575219f,104.043358337f),15));

                mapboxMap.setMinZoomPreference(15);
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();

        mapView.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();

        mapView.onPause();
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);

        mapView.onSaveInstanceState(outState);
    }

    @Override
    protected void onStart() {
        super.onStart();

        mapView.onStart();
    }

    @Override
    protected void onStop() {
        super.onStop();

        mapView.onStop();
    }

    @Override
    public void onLowMemory() {
        super.onLowMemory();

        mapView.onLowMemory();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

        mapView.onDestroy();
    }
}
